package com.gimi.cloud.jimi.manage.controller.userInfo;

import com.gimi.cloud.jimi.common.enums.ResponseCodeEnum;
import com.gimi.cloud.jimi.common.utils.JsonUtil;
import com.gimi.cloud.jimi.common.utils.Page;
import com.gimi.cloud.jimi.common.utils.reqhead.Response;
import com.gimi.cloud.jimi.common.utils.reqhead.Util;
import com.gimi.cloud.jimi.dto.bank.BankCardDTO;
import com.gimi.cloud.jimi.dto.userInfo.RdEmergencyContactDTO;
import com.gimi.cloud.jimi.dto.userInfo.RdUserInfoDTO;
import com.gimi.cloud.jimi.dto.userInfo.RdWorkInfoDTO;
import com.gimi.cloud.jimi.dto.userInfo.UserAppInformationDTO;
import com.gimi.cloud.jimi.entity.userInfo.RdUserInfoEntity;
import com.gimi.cloud.jimi.manage.service.app.UserAppInformationService;
import com.gimi.cloud.jimi.manage.service.userInfo.BankCardService;
import com.gimi.cloud.jimi.manage.service.userInfo.EmergencyContactService;
import com.gimi.cloud.jimi.manage.service.userInfo.UserInfoService;
import com.gimi.cloud.jimi.manage.service.userInfo.WorkInfoService;
import com.github.pagehelper.PageInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户详细信息管理
 * @author chendi
 * @date 2017年12月27日
 */
@RestController
@RequestMapping(value = "userInfo")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private EmergencyContactService emergencyContactService;
    @Autowired
    private WorkInfoService workInfoService;
    @Autowired
    private BankCardService bankCardService;
    @Autowired
    private UserAppInformationService userAppInformationService;

    /** 获取所有user详情列表 */
    @RequestMapping(value = "/list")
    public void list(@RequestBody RdUserInfoDTO userInfoDTO, HttpServletResponse response) {
        Response resp = new Response();
        List<RdUserInfoEntity> list = userInfoService.getUsreInfoList();
        resp.setRes_data(list);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        Util.sendResponse(JsonUtil.toJson(resp), response);
    }

    /** 查询用户分页列表 */
    @RequestMapping(value = "/pageList")
    public void pageList(@RequestBody RdUserInfoDTO userInfoDTO, HttpServletResponse response) {
    	Response resp = new Response();
    	PageInfo<RdUserInfoDTO> list = userInfoService.getUserInfoPageList(userInfoDTO);
        resp.setRes_data(list);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        Util.sendResponse(JsonUtil.toJSONStringWithDateFormatYYYYMMDDHHMMSS(resp), response);
    }

    /** 查询单个user信息 */
    @RequestMapping(value = "/single")
    public void single(@RequestBody RdUserInfoDTO userInfoDTO, HttpServletResponse response) {
        Response resp = new Response();
        RdUserInfoDTO data = userInfoService.getUserInfoSingle(userInfoDTO);
        resp.setRes_data(data);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        Util.sendResponse(JsonUtil.toJson(resp), response);
    }


    @RequestMapping(value = "/update")
    public void updateUserInfo(@RequestBody RdUserInfoDTO userInfoDTO, HttpServletResponse response) {
        Response resp = new Response();
        userInfoService.updateUserInfo(userInfoDTO);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        Util.sendResponse(JsonUtil.toJson(resp), response);
    }

    @RequestMapping(value = "/userIsCanBeUsed")
    public void userIsCanBeUsed(@RequestBody RdUserInfoDTO userInfoDTO, HttpServletResponse response) {
        Response resp = new Response();
        userInfoService.userIsCanBeUsed(userInfoDTO);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        Util.sendResponse(JsonUtil.toJson(resp), response);
    }

    /**
     * 用户基础资料
     * @param userInfoDTO
     * @param response
     */
    @RequestMapping(value = "/detail")
    public void userDetail(@RequestBody RdUserInfoDTO userInfoDTO, HttpServletResponse response) {
        Response resp = new Response();
        Integer userId = userInfoDTO.getUserId();
        Map<String, Object> data = new HashMap<>();
        RdUserInfoDTO userInfo = userInfoService.getUserInfoSingle(userInfoDTO);
        RdEmergencyContactDTO emeDto = new RdEmergencyContactDTO();
        emeDto.setUserId(userId);
        RdEmergencyContactDTO eme = emergencyContactService.findEmergencyContactByUser(emeDto);
        RdWorkInfoDTO workParam = new RdWorkInfoDTO();
        workParam.setUserId(userId);
        RdWorkInfoDTO workInfo = workInfoService.getUserWorkInfo(workParam);
        BankCardDTO bankParam = new BankCardDTO();
        bankParam.setUserId(userId);
        BankCardDTO bankCard = bankCardService.getUserBankCard(bankParam);
        UserAppInformationDTO appinfoParam = new UserAppInformationDTO();
        appinfoParam.setPageSize(5);
        appinfoParam.setCurrentPage(0);
        appinfoParam.setUserId(userInfoDTO.getUserId());
        Page<UserAppInformationDTO> appInfoPage = userAppInformationService.getUserAppInformationPage(appinfoParam);

        data.put("appInfoPage",appInfoPage);
        data.put("userInfo", userInfo);
        data.put("emergencyContact", eme);
        data.put("workInfo", workInfo);
        data.put("bankCard", bankCard);
        resp.setRes_data(data);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        Util.sendResponse(JsonUtil.toJson(resp), response);
    }

    @RequestMapping(value = "/userAppInfoList")
    public void userAppInfoList(@RequestBody UserAppInformationDTO appinfoParam, HttpServletResponse response) {
        //UserAppInformationDTO appinfoParam = new UserAppInformationDTO();
        Page<UserAppInformationDTO> appInfoPage = userAppInformationService.getUserAppInformationPage(appinfoParam);

        Response resp = new Response();
        resp.setRes_data(appInfoPage);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        Util.sendResponse(JsonUtil.toJson(resp), response);
    }
}
